/*
 * int_module.c
  *  Arizona State University
 *  CSE 325: Embedded Systems Programming
 *  Lab 7: Morse Code
 *  Georgii Tkachuk, Brentton Garber
 *   Emails: 	gtkachuk@asu.edu btgarber@asu.edu
 *      Description:	Configures 4 IRS's; Two PIT IRS's (one for refresh rate and the next step timing delay, one for calling reads from the nunchuk)
 *      									Two GPT IRS's (one for reset button and one for adding ghosts to the game)
 *      				
 */
#include "common.h"

// get the vector ram
extern uint32 __VECTOR_RAM[];
int screen_count;
//int program_mode;
// Initialize the handler for refreshing the LED Matrix
void set_pit_handler(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	// Unmask the 55th slot
	MCF_INTC0_IMRH &= ~(1) << 23;
	//set PIT's (55) interrupt level to 5 and priority to 7
	MCF_INTC0_ICR55 |= (5) << 3;
	MCF_INTC0_ICR55 |= (7);
}
// Initialize the handler for updating the game sequence
void set_pit_handler_1(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	// Unmask the 56th slot
	MCF_INTC0_IMRH &= ~(1) << 24;
	//set PIT's (56) interrupt level to 5 and priority to 7
	MCF_INTC0_ICR56 |= (4) << 3;
	MCF_INTC0_ICR56 |= (2);
}
// Initialize the handler for stopping the game
void set_gpt_handler(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	MCF_INTC0_IMRH &= ~(1) << 12;
	MCF_INTC0_ICR44 |= (5) << 3;
	MCF_INTC0_ICR44 |= (6);
}
// Initialize the handler for stopping the game
void set_gpt_handler_1(int vector, void (*handler)(void))
{
	// Set the Interrupt handler function
	__VECTOR_RAM[vector]=(uint32)handler;
	
	MCF_INTC0_IMRH &= ~(1) << 13;
	MCF_INTC0_ICR45 |= (5) << 3;
	MCF_INTC0_ICR45 |= (6);
}

// This PIT timer is responsible for both refreshing the LED matrix screen and refreshing the game unit positions
__declspec(interrupt) void pit_handler(void)
{
	// Clear the interrupt flag
	MCF_PIT0_PCSR |= 1 << 2;
	
	// track game time
	game_trackTime();
	
	// refresh the led matrix
	ledmatrix_refresh();
}

// Interrupt handler for updating the game sequence
__declspec(interrupt) void pit_handler_1(void)
{
	// Clear the interrupt flag
	MCF_PIT1_PCSR |= 1 << 2;

	// Set the next game sequence to the LED Matrix
	nunchuk_read();
}
// Interrupt handler to start the game
__declspec(interrupt) void gpt_handler(void)
{
	// Clear the interrupt flag
	MCF_GPT_GPTFLG1 |= 1;
	
	// add a ghost to the game board
	//game_modGhost();
}

// Interrupt handler to stop the game
__declspec(interrupt) void gpt_handler_1(void)
{
	
	// Clear the interrupt flag
	MCF_GPT_GPTFLG1 |= 7;
	
	// Stop the game
	//game_reset();
}

